package com.bjyhl.core.fragments.bottom;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.bjyhl.core.R;
import com.bjyhl.core.fragments.BaseFragment;
import com.bjyhl.core.fragments.LatteFragment;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.joanzapata.iconify.widget.IconTextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import me.yokeyword.fragmentation.ISupportFragment;

/**
 * Created by Natalya on 2019/1/27 14:28
 * Note:
 */

public abstract class BaseBottomFragment extends LatteFragment implements View.OnClickListener {

    private static final String TAG = "BaseBottomFragment";
    private final ArrayList<BottomTabBean> TAB_BEANS = new ArrayList<>();
    private final ArrayList<BottomItemFragment> ITEM_FRAGMENTS = new ArrayList<>();
    private final ArrayList<String> ITEM_FRAGMENTS_TITLE = new ArrayList<>();
    private final LinkedHashMap<BottomTabBean, BottomItemFragment> ITEMS = new LinkedHashMap<>();

    private int mCurrentFragment = 0;
    private int mIndexFragment = 0;
    private int mClickedColor = Color.RED;
    private LinearLayoutCompat mBottomBar = null;
    private AppCompatTextView btn_root_logo;
    private AppCompatTextView base_frag_title;
    private IconTextView btn_root_back;

    @Override
    public Object setLayout() {
        return R.layout.fragment_bottom;
    }

    public ArrayList<BottomItemFragment> getItemFragments() {
        return ITEM_FRAGMENTS;
    }

    public abstract LinkedHashMap<BottomTabBean, BottomItemFragment> setItems(BottomItemBuilder builder);

    public abstract int setIndexFragment();

    public void showBackBtn(boolean isVisible) {
        if(!isVisible)
            btn_root_back.setVisibility(View.INVISIBLE);
        else
            btn_root_back.setVisibility(View.VISIBLE);
    }

    @ColorInt
    public abstract int setClickColor();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIndexFragment = setIndexFragment();
        if (setClickColor() != 0) {
            mClickedColor = setClickColor();
        }

        final BottomItemBuilder builder = BottomItemBuilder.builder();
        final LinkedHashMap<BottomTabBean, BottomItemFragment> items = setItems(builder);
        ITEMS.putAll(items);
        for (Map.Entry<BottomTabBean, BottomItemFragment> item : ITEMS.entrySet()) {
            final BottomTabBean key = item.getKey();
            final BottomItemFragment value = item.getValue();
            TAB_BEANS.add(key);
            ITEM_FRAGMENTS.add(value);
            ITEM_FRAGMENTS_TITLE.add("");
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        mBottomBar = $(R.id.left_bar);
        final int size = ITEMS.size();
        for (int i = 0; i < size; i++) {
            //填充导航按钮
            LayoutInflater.from(getContext()).inflate(R.layout.bottom_item_icon_text_layout, mBottomBar);
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            //设置每个item的点击事件
            item.setTag(i);
            item.setOnClickListener(this);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
            final BottomTabBean bean = TAB_BEANS.get(i);
            //初始化底部数据
            itemIcon.setText(bean.getIcon());
            itemTitle.setText(bean.getTitle());
            if (i == mIndexFragment) {
                item.setBackgroundColor(mClickedColor);
                itemIcon.setTextColor(Color.WHITE);
                itemTitle.setTextColor(Color.WHITE);
            } else {
                item.setBackgroundColor(Color.WHITE);
                itemIcon.setTextColor(Color.BLACK);
                itemTitle.setTextColor(Color.BLACK);
            }
        }
        final ISupportFragment[] delegateArray = ITEM_FRAGMENTS.toArray(new ISupportFragment[size]);
        getSupportDelegate().loadMultipleRootFragment(R.id.right_bar_delegate_container, mIndexFragment, delegateArray);

        btn_root_logo = $(R.id.btn_root_logo);
        base_frag_title = $(R.id.base_frag_title);
        btn_root_back = $(R.id.btn_root_back);
        setTitle("");
        btn_root_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("登录。。。");
            }
        });
        btn_root_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int deep = 0;
                BottomItemFragment fragment = ITEM_FRAGMENTS.get(mCurrentFragment);
                int childFragmentsSize = 0;

                BaseFragment childFragment = fragment;
                while (true) {
                    deep++;
                    childFragmentsSize = childFragment.getChildFragmentManager().getFragments().size();
                    if(childFragmentsSize > 1) {
                        childFragment = (BaseFragment) childFragment.getChildFragmentManager().getFragments().get(1);
                    } else {
                        break;
                    }
                }

                if(deep > 1 || childFragmentsSize > 1) {
                    ((BaseFragment)childFragment.getParentFragment()).getSupportDelegate().popChild();
                }
                if(deep > 2 || childFragmentsSize > 2) {
                    showBackBtn(true);
                } else {
                    showBackBtn(false);
                    setTitle("");
                }
            }
        });

//        TextClock tClock = $(R.id.tClock);
//        tClock.setFormat24Hour("yyyy-MM-dd hh:mm:ss,EEEE");
    }

    private void resetColor() {
        final int count = mBottomBar.getChildCount();
        for (int i = 0; i < count; i++) {
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            item.setBackgroundColor(Color.WHITE);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            itemIcon.setTextColor(Color.BLACK);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
            itemTitle.setTextColor(Color.BLACK);
        }
    }

    public void changeColor(int tabIndex) {
        resetColor();
        final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(tabIndex);
        item.setBackgroundColor(mClickedColor);
        //获取底部icon
        final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
        itemIcon.setTextColor(Color.WHITE);
        //获取底部导航文字
        final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
        itemTitle.setTextColor(Color.WHITE);
    }

    @Override
    public void onClick(View view) {
        final int tabIndex = (int) view.getTag();
        if (tabIndex != mCurrentFragment) {
            changeColor(tabIndex);
            getSupportDelegate().showHideFragment(ITEM_FRAGMENTS.get(tabIndex), ITEM_FRAGMENTS.get(mCurrentFragment));
            //注意先后顺序
            mCurrentFragment = tabIndex;
            showTitle(ITEM_FRAGMENTS_TITLE.get(mCurrentFragment));
        }
    }

    public void setTitle(String title) {
        ITEM_FRAGMENTS_TITLE.set(mCurrentFragment, title);
        showTitle(title);
    }
    private void showTitle(String title) {
        if(StringUtils.isEmpty(title))
            base_frag_title.setText("构建和谐医患关系新媒体宣传平台");
        else
            base_frag_title.setText(title);
    }

    public void setCurrentFragment(int currentFragment) {
        mCurrentFragment = currentFragment;
    }
}
